package org.jeecg.modules.pollutionStatus.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.enterpriseBasicInformation.entity.EnterpriseBasicInformation;
import org.jeecg.modules.pollutionStatus.entity.PollutionStatus;
import org.jeecg.modules.pollutionStatus.service.IPollutionStatusService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 重污染天气配置表
 * @Author: jeecg-boot
 * @Date:   2024-04-12
 * @Version: V1.0
 */
@Api(tags="重污染天气配置表")
@RestController
@RequestMapping("/pollutionStatus/pollutionStatus")
@Slf4j
public class PollutionStatusController extends JeecgController<PollutionStatus, IPollutionStatusService> {
	@Autowired
	private IPollutionStatusService pollutionStatusService;
	@Autowired
	private ISysDepartService sysDepartService;
	
	/**
	 * 分页列表查询
	 *
	 * @param pollutionStatus
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "重污染天气配置表-分页列表查询")
	@ApiOperation(value="重污染天气配置表-分页列表查询", notes="重污染天气配置表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PollutionStatus pollutionStatus,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PollutionStatus> queryWrapper = QueryGenerator.initQueryWrapper(pollutionStatus, req.getParameterMap());
		Page<PollutionStatus> page = new Page<PollutionStatus>(pageNo, pageSize);
		IPage<PollutionStatus> pageList = pollutionStatusService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @AutoLog(value = "重污染天气配置表-按公司名称查询")
	 @ApiOperation(value="重污染天气配置表-按公司名称查询", notes="重污染天气配置表-按公司名称查询")
	 @GetMapping(value = "/getPollutionStatus")
	 public Result<?> getPollutionStatus() {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<SysDepart> sysDeparts = sysDepartService.queryUserDeparts(loginUser.getId());
		 List<String> departNames = sysDeparts.stream()
				 .filter(item -> item.getOrgType().equals("1"))
				 .map(SysDepart::getDepartName)
				 .collect(Collectors.toList());
		 if(departNames==null||departNames.size()==0){
			 return Result.error("未找到公司信息，请联系管理员");
		 }
		 QueryWrapper<PollutionStatus> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("company_name", departNames.get(0));
		 PollutionStatus pollutionStatus = pollutionStatusService.getOne(queryWrapper);
		 if (Objects.isNull(pollutionStatus)){
			 pollutionStatus = new PollutionStatus();
			 pollutionStatus.setCompanyName(departNames.get(0));
			 pollutionStatus.setStatus("0");
			 pollutionStatusService.save(pollutionStatus);
		 }
		 return Result.OK(pollutionStatus);
	 }

	 /**
	  *  编辑--按公司名称编辑
	  *
	  * @param pollutionStatus
	  * @return
	  */
	 @AutoLog(value = "重污染天气配置表-对外-按公司名称编辑")
	 @ApiOperation(value="重污染天气配置表-对外-按公司名称编辑", notes="重污染天气配置表-对外-按公司名称编辑")
	 @PutMapping(value = "/m/editPollutionStatus")
	 public Result<?> editPollutionStatus(@RequestBody PollutionStatus pollutionStatus) {
		 LambdaUpdateWrapper<PollutionStatus> lqw = Wrappers.lambdaUpdate(PollutionStatus.class);
		 lqw.eq(PollutionStatus::getCompanyName, pollutionStatus.getCompanyName());
		 lqw.set(PollutionStatus::getStatus, pollutionStatus.getStatus());
		 boolean update = pollutionStatusService.update(lqw);
		 if (!update){
			 return Result.error("编辑失败, 请核对参数!");
		 }
		 return Result.OK("编辑成功!");
	 }
	
	/**
	 *   添加
	 *
	 * @param pollutionStatus
	 * @return
	 */
	@AutoLog(value = "重污染天气配置表-添加")
	@ApiOperation(value="重污染天气配置表-添加", notes="重污染天气配置表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PollutionStatus pollutionStatus) {
		pollutionStatusService.save(pollutionStatus);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param pollutionStatus
	 * @return
	 */
	@AutoLog(value = "重污染天气配置表-编辑")
	@ApiOperation(value="重污染天气配置表-编辑", notes="重污染天气配置表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PollutionStatus pollutionStatus) {
		pollutionStatusService.updateById(pollutionStatus);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "重污染天气配置表-通过id删除")
	@ApiOperation(value="重污染天气配置表-通过id删除", notes="重污染天气配置表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		pollutionStatusService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "重污染天气配置表-批量删除")
	@ApiOperation(value="重污染天气配置表-批量删除", notes="重污染天气配置表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.pollutionStatusService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "重污染天气配置表-通过id查询")
	@ApiOperation(value="重污染天气配置表-通过id查询", notes="重污染天气配置表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PollutionStatus pollutionStatus = pollutionStatusService.getById(id);
		if(pollutionStatus==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(pollutionStatus);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param pollutionStatus
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PollutionStatus pollutionStatus) {
        return super.exportXls(request, pollutionStatus, PollutionStatus.class, "重污染天气配置表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PollutionStatus.class);
    }

}
